/*9D1QA5jkDSXDuuPdOG3mwLOEQFOIyRQfYymJ2L8tsm/NQ02F+KABa26jzEvUajwM*/
import { Cipher, viewModel, gzip } from 'nc-lightapp-front';
const { getGlobalStorage, getCookie } = viewModel;

export function bytesToSize(bytes, fixed, unit) {
	fixed = fixed || 2;
	if (bytes == 0 || typeof bytes === 'undefined' || !isRealNum(bytes))
		return fixed > 0 ? `0.${Array(fixed + 1).join('0')}B` : '0B';
	var k = 1024,
		sizes = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ],
		i = isWrong(unit) ? Math.floor(Math.log(bytes) / Math.log(k)) : unit;
	return (bytes / Math.pow(k, i)).toFixed(fixed) + sizes[i];
}

export function isRealNum(val) {
	var pattern = /^(-)?\d+(\.\d+)?$/;
	return pattern.exec(val) == null || val === '';
}

export function isWrong(param) {
	return typeof param === 'undefined' || param === null || param === '';
}

//
export function getAesKey() {
	let cckk = getCookie('cookiets') || Date.now(); //双cookie验证 liuxis
	// 做一次 数据转型
	cckk = isNaN(cckk) ? cckk : Number(cckk);
	// 加密 bbqin
	let aeskey = Cipher.opaqueDecrypt(getGlobalStorage('localStorage', 'cowboy'));
	let cckks = cckk + '';
	aeskey = cckks + aeskey.substring(0, aeskey.length - cckks.length);
	return aeskey;
}

/**
 * 解密返回值
 * @param {*} response 
 */
export function decryptResponse(response) {
	// 1. 解密
	if (Cipher.cipherFlag || getGlobalStorage('localStorage', 'rockin') === 'true') {
		let aeskey = getAesKey();
		response = typeof response === 'string' ? Cipher.decrypt(response, aeskey) : response;
	}
	// 2. 解压
	let isGzip = getGlobalStorage('localStorage', 'gzip');
	if (isGzip == 1) {
		response = new gzip().unzip(response);
	}
	return response;
}

/*9D1QA5jkDSXDuuPdOG3mwLOEQFOIyRQfYymJ2L8tsm/NQ02F+KABa26jzEvUajwM*/